package svgedit.gui.command;

import svgedit.gui.View;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGLength;
import svgedit.svg.SVGStylable;

/**
 *  Define change stroke width execution as a command object which has 
 *      execute (redo) and undo methods
 * */
public class StrokeWidthChangeCommand implements Command
{
	private View view;
	private SVGLength strokeWidth;
	private SVGLength backupLength;
	private SVGLength changeLength;
	private SVGElement backupElem;
	private SVGElement element;
 	
	/** Constructor for stroke width change command object
	 * 
	 * @param view to get the view panel
	 * @param strokeWidth to get the new stroke width for the item
	 * */
	public StrokeWidthChangeCommand(View view,SVGElement element,SVGLength strokeWidth ){
		this.view = view;
		this.element = element;
		this.strokeWidth = strokeWidth;
		backupLength = new SVGLength();
		changeLength = new SVGLength();
		backupElem = null;
	}
	
	/**
	 *  Change items stroke width when execute
	 * */
	public void execute(){
		if(backupElem == null){
			backupLength.setValueFromLength(((SVGStylable)element).getStrokeWidth());
			changeLength.setValueFromLength(strokeWidth);
			backupElem = element;
			((SVGStylable) element).getStrokeWidth().setValueFromLength(strokeWidth);
		}
		else{
			((SVGStylable) backupElem).getStrokeWidth().setValueFromLength(changeLength);
			view.repaint();
		}
	}
	
	/**
	 *  Change items stroke width back using backupPaint
	 * */
	public void undo(){
		((SVGStylable) backupElem).getStrokeWidth().setValueFromLength(backupLength);
		view.repaint();
	}
     
}
